 $.extend(planner.dialogs, {
    copyMech: {
        show: function(){
            $("#txtCopyThisMechUrl" ).val("");
            $("#CopyThisMechError").css("display", "none");
            $( "#dialog-copy-mech" ).dialog({
                height: 265,
                width: 620,
                modal: true,
            });    
        },
        copy_this_mech: function()
        {
            $.ajax({ 
                url: "/m",
                dataType: 'json',
                type: "GET",
                data: { u: $("#txtCopyThisMechUrl").val(),  }
            }).done( function(data) {
                if (data && data.mechtype )
                {
                    on = planner.mech.name;
                    ot = planner.mech.mechtype;
                    oe = planner.mech.equipment;
                    ow = planner.mech.weapons;
                    om = planner.mech.modules;
        
                    
                    planner.mech.name = $("#txtCopyThisMechName").val();
                    planner.mech.speed = data.speed;
                    planner.mech.mechtype = data.mechtype ;
                    planner.mech.equipment = data.equipment;
                    planner.mech.weapons = data.weapons;
                    planner.mech.modules = [];
                    // planner.drop.ui.update();
 
                    $( "#dialog-copy-mech" ).dialog("close");

                    console.log("planner.mech.save()");
                    args = { i:"1", a:"s", n: planner.mech.name };
                    planner.server.post_mech(
                        args,
                        function (html) {
                            planner.mech.mechtype = ot;
                            planner.mech.equipment = oe;
                            planner.mech.weapons = ow;
                            planner.mech.modules = om;
                            args = { i:"1", a:"s" };
                            planner.server.post_mech(
                                args,
                                function (html) {
                                    $(".post").hide();
                                    // alert("2");
                                    setTimeout(function() {
                                        planner.garage.ui.update();    
                                    }, 100)
                                    
                                },
                                function (html) { },
                                false
                            );
                        },
                        function (html) { },
                        false
                    );    
    
                
                     
                }
                else
                {
                    $("#CopyThisMechError").css("display", "block");
                }      
            }).fail(function(){
                alert("bbad");
                $( "#dialog-copy-mech" ).dialog("close"); 
            });
        },
        ui:
        {
            setup: function()
            {
                
            },
            init: function()
            {
                $('#txtCopyThisMechName').keypress(function(e) {
                    if (e.keyCode == $.ui.keyCode.ENTER) {
                        planner.dialogs.copyMech.copy_this_mech();
                    }
                });
                
                $("#btnCopyThisMech").button().click( planner.dialogs.copyMech.copy_this_mech );
                
                $("#btnCancelCopyThisMech").button().click(
                    function () {
                        $( "#dialog-copy-mech" ).dialog("close");                
                    }
                );
                
                $(document).on("dialogopen", "#dialog-copy-mech", function(event, ui) {
                    setTimeout(function() {
                        $("#txtCopyThisMechName").focus();    
                    },100);
                });
                
                
                $("#btnCopyThisMechRandomName").click(
                    function () { 
                        $.ajax({ url: "/n" }).done( function(data) {
                             $("#txtCopyThisMechName").val(data);  
                        });
                    }
                );
            }
        }
    }
});